//
//  SetPersonMessageVC.m
//  LoveTrail
//
//  Created by giantumbrella on 15/6/11.
//  Copyright (c) 2015年 赵娟. All rights reserved.
//
//还缺少的是label显示信息的label


#import "SetPersonMessageVC.h"
#import "CommonMacro.h"
#import "EditMessagePerVC.h"
@interface SetPersonMessageVC ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,valuePassToUpViewDelgate>
{
    NSArray *dataArray;
    NSArray *titleArray;
    NSArray *babyArray;
    UIImageView *iconImageView;
    NSString *tempValue;
}
@property (nonatomic,strong) UIButton *changeButton;
@end

@implementation SetPersonMessageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人中心";
    
    //添加表格视图
    [self addSubViews];

    dataArray = @[@"",@"昵称",@"真实姓名",@"性别",@"生日",@"手机",@"地区",@"邮编"];
    titleArray = @[@"宝宝信息："];
    babyArray = @[@"年龄",@"性别",@"生日"];
}

#pragma mark 创建tableView 
- (void)addSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.9)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    
    //添加一个按钮 退出当前账号
    CGFloat Y = self.tableView.frame.size.height+self.tableView.frame.origin.y+10;
    UIButton *exitButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.3, Y, SCREEN_WIDTH*0.5, SCREEN_HEIGHT-Y-14)];
    exitButton.backgroundColor = UIColorWithRGBA(247, 93, 133, 1);
    [exitButton setBackgroundImage:[UIImage imageNamed:@"退-出-账-号-点击后@2x"] forState:UIControlStateNormal];
    exitButton.layer.cornerRadius = 5;
    exitButton.clipsToBounds = YES;
    [self.view addSubview:exitButton];
}

#pragma mark 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return dataArray.count;
    }
    else
    {
        return babyArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *ID = @"Cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
            iconImageView.backgroundColor = [UIColor redColor];
            iconImageView.layer.cornerRadius = 25;
            iconImageView.clipsToBounds = YES;
            [cell addSubview:iconImageView];
            
            self.changeButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.7, iconImageView.frame.size.height*0.4, SCREEN_WIDTH*0.3, iconImageView.frame.size.height*0.8)];
            [self.changeButton setTitle:@"更换头像" forState:UIControlStateNormal];
            [self.changeButton setTitleColor:UIColorWithRGBA(159, 159, 159, 1)forState:UIControlStateNormal];
//            [changeButton setBackgroundColor:[UIColor redColor]];
            [self.changeButton.titleLabel setFont:[UIFont systemFontOfSize:13]];
            [cell addSubview:self.changeButton];
           
            [self.changeButton addTarget:self action:@selector(changeIconButton) forControlEvents:UIControlEventTouchUpInside];
            
        }else{
        cell.textLabel.text = dataArray[indexPath.row];
            
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.3, 8, SCREEN_WIDTH*0.6, 30)];
            label.text = tempValue;
            [cell addSubview:label];
        }
    }
    else
    {
        cell.textLabel.text = babyArray[indexPath.row];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.3, 8, SCREEN_WIDTH*0.6, 30)];
        label.text = tempValue;
        [cell addSubview:label];

    }
    
    cell.textLabel.textColor = UIColorWithRGBA(146, 146, 146, 1);
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section==1) {
        return @"宝宝信息：";
    }else
    {
        return nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return;
    }else{
    EditMessagePerVC *editMessage = [[EditMessagePerVC alloc] init];
    editMessage.delegate = self;
    [self.navigationController pushViewController:editMessage animated:YES];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            return 70;
        }
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==1){
        return 30;
    }else
    {
        return 0;
    }
}


#pragma mark 更换头像
- (void)changeIconButton
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请选择图片来源" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
    alertView.delegate = self;
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1) {
        UIImagePickerController *ipc=[[UIImagePickerController alloc]init];
        //判断当前设备能否拍照
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            return;
        }
        ipc.sourceType=UIImagePickerControllerSourceTypeCamera;
        ipc.delegate=self;
        ipc.allowsEditing=YES;
        [self presentViewController:ipc animated:YES completion:nil];
    }
    else if (buttonIndex==2){
        UIImagePickerController *ipc=[[UIImagePickerController alloc]init];
        ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        ipc.delegate=self;
        ipc.allowsEditing=NO;
        [self presentViewController:ipc animated:YES completion:nil];
    }

}

#pragma mark imagePickeControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    //把图片保存到相册,保存完了执行image:didFinishSavingWithError:contextInfo:方法
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    iconImageView.image=image;
}

#pragma mark valuePassToUpViewDelgate
- (void)valuePassToUpView:(NSString *)value
{
    
    tempValue = value;
    [self.tableView reloadData];
}
@end
